package com.sharding.quickStart;

import com.alibaba.fastjson.JSON;
import com.sharding.entity.Goods;
import com.sharding.service.GoodsService;
import com.sharding.utils.DateTimeUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang.time.DateUtils;
import org.apache.commons.lang3.RandomUtils;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.Date;
import java.util.List;

/**
 * Desc:
 * Date:    2023/2/27
 *
 * @author WanYangWei
 */

@RunWith(SpringRunner.class)
@SpringBootTest
@Slf4j
public class IntervalShardingDemo {
    @Autowired
    private GoodsService goodsService;

    /**
     * 新增数据1
     */
    @Test
    public void dateIntervalInsert1() {
        //localDateTime 如果s为0时,字符串原本应为 2023-01-01T01:01:00 而是2023-01-01T01:01
//        LocalDateTime curDate = LocalDateTime.of(2023, 1, 1, 1, 1, 23);
        for (int i = 0; i < 3; i++) {
            Goods goods = new Goods();
            goods.setId(RandomUtils.nextInt());
            goods.setName("产品:" + i);
            goods.setCreateTime(new Date());
            goods.setUpdateTime(new Date());
            goodsService.saveGoodsInfo(goods);
//            curDate = curDate.plusMonths(1);
        }
    }

    /**
     * 新增数据2
     */
    @Test
    public void dateIntervalInsert2() {
        Date date = null;
        try {
            date = DateTimeUtils.parseDateTimeStr("2023-01-01 01:01:01");
        } catch (Exception exception) {
            log.info("解析时间格式错误");
            return;
        }
        for (int i = 0; i < 12; i++) {
            Goods goods = new Goods();
            goods.setId(RandomUtils.nextInt());
            goods.setName("产品:" + i);
            goods.setCreateTime(date);
            goods.setUpdateTime(new Date());
            goodsService.saveGoodsInfo(goods);
            date = DateUtils.addMonths(date, 1);
        }
    }

    @Test
    public void queryByRangeTimeTest() {
        String beginTime = "2023-01-01 01:01:01";
        String endTime = "2023-12-01 01:01:01";
        List<Goods> goods = goodsService.rangeByCreateTime(beginTime, endTime);
        log.info("getGoodsInfo:size:{}-{}", CollectionUtils.isEmpty(goods) ? 0 : goods.size(), goods);
    }

    @Test
    public void getById() {
        long id = 1631492436;
        Goods goodsInfo = goodsService.getGoodsInfo(id);
        log.info("getById:{}", JSON.toJSONString(goodsInfo));

    }

    @Test
    public void updateById() {
        Goods goodsInfo = new Goods();
        goodsInfo.setId(1631492436);
        goodsInfo.setName("更新test");
        int row = goodsService.updateById(goodsInfo);
        log.info("updateNameById :{}", row);
    }

    @Test
    public void queryByRangeTimeTest2() {
//        String beginTime = "2023-05-01 01:01:01";
//        String endTime = "2023-05-02 01:01:01";
        String beginTime = "2023-04-01 01:01:01";
        String endTime = "2023-05-01 01:01:01";
        List<Goods> goods = goodsService.rangeByCreateTime(beginTime, endTime);
        log.info("getGoodsInfo:size:{}-{}", CollectionUtils.isEmpty(goods) ? 0 : goods.size(), goods);
    }
}
